
package ventanas;

import baseDeDatosJDBC.AdministradorDeConexiones;
import clases.TFnumericos;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class SeleccionarAnio extends javax.swing.JInternalFrame {
    
    public static boolean abierta = false;

    public SeleccionarAnio() {
        initComponents();
        abierta = true;
        getContentPane().setBackground(new java.awt.Color(205, 200, 177));

        //Definimos la dimension de la ventana hija.
        setPreferredSize(new java.awt.Dimension(450, 340));

        //Posicionamos la ventana interna en la esquina izquierda.
        setLocation(180, 50);
        pack();
        limitarTextField(jTextField1, 4);
    }
    
    
    Calendar fecha = new GregorianCalendar();
    int anioActual = fecha.get(Calendar.YEAR);

    public static void limitarTextField(JTextField textField, int maximo) {
        textField.setDocument(new TFnumericos(textField, maximo));
    }


    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(205, 200, 177));
        setClosable(true);
        setTitle("Seleccionar Año");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosed(evt);
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jPanel1.setBackground(new java.awt.Color(205, 200, 177));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Seleccione el año a ingresar", 2, 0, new java.awt.Font("Tahoma", 1, 12), new java.awt.Color(0, 0, 0))); // NOI18N

        jLabel1.setText("Ingrese el año que desea seleccionar:");

        jButton1.setBackground(new java.awt.Color(205, 200, 177));
        jButton1.setText("Seleccionar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("4 Caracteres");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1))
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(205, 200, 177));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Recordatorio", 2, 0, new java.awt.Font("Tahoma", 1, 12), new java.awt.Color(0, 0, 0))); // NOI18N

        jLabel2.setText("Recuerde verificar el año de su ordenador, debe ser el actual.");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(33, Short.MAX_VALUE)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(49, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 8, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosed
        abierta = false;
    }//GEN-LAST:event_formInternalFrameClosed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
 
        if (jTextField1.getText().length() < 4) {

            JOptionPane.showMessageDialog(this, "El año debe tener 4 caracteres", "Advertencia", 0,
                    new javax.swing.ImageIcon(getClass().getResource("/imagenes/informacion.png")));
            jTextField1.requestFocus();
            jTextField1.selectAll();
        } else if ((Integer.parseInt(jTextField1.getText()) < 1900) || (Integer.parseInt(jTextField1.getText()) > anioActual)) {

            JOptionPane.showMessageDialog(this, "El año no puede ser mayor a " + anioActual + " ni menor a 1900", "Información", 0,
                    new javax.swing.ImageIcon(getClass().getResource("/imagenes/informacion.png")));
            jTextField1.requestFocus();
            jTextField1.selectAll();

        } else {
            String query="select * from siesca.datos"+jTextField1.getText()+"";
            try {

                Connection conn = AdministradorDeConexiones.obtenerConexion();
                Statement st = conn.createStatement();
                ResultSet rs = st.executeQuery(query);
                
                rs.close();
                st.close();
                conn.close();

                int result = JOptionPane.showConfirmDialog(this, "Año localizado "
                        + "desea cambiar el año?", "Confirmación", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, new javax.swing.ImageIcon(getClass().getResource("/imagenes/advertenciaDI.png")));

                if (result == JOptionPane.YES_OPTION) {

                    try {

                        FileWriter escritorArchivo = new FileWriter("anio.txt");
                        BufferedWriter bw = new BufferedWriter(escritorArchivo);
                        PrintWriter annio = new PrintWriter(bw);
                        annio.write("" + jTextField1.getText() + "");

                        JOptionPane.showMessageDialog(this, "El año ha sido seleccionado", "Confirmación", 0,
                                new javax.swing.ImageIcon(getClass().getResource("/imagenes/informacion.png")));

                        
                        annio.close();
                        bw.close();

                    } catch (IOException ex) {

                        JOptionPane.showMessageDialog(this, "No se pudo seleccionar el año, problema en la escritura de archivo de recursos", "Advertencia", 0,
                                new javax.swing.ImageIcon(getClass().getResource("/imagenes/advertenciaDI.png")));
                    }
                } else {

                    jTextField1.requestFocus();
                    jTextField1.selectAll();
                }
            } catch (Exception er) {

                JOptionPane.showMessageDialog(this, "El año " + jTextField1.getText() + " No existe en la base!", "Advertencia", 0,
                        new javax.swing.ImageIcon(getClass().getResource("/imagenes/advertenciaDI.png")));
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
